package com.taomeng.servlet;

import com.taomeng.constant.LeaveTypeConstant;
import com.taomeng.entity.Student;
import com.taomeng.service.StudentService;
import com.taomeng.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@WebServlet("/leave")
public class LeaveServlet extends HttpServlet {

    @Autowired
    private StudentService studentService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int leaveType = Integer.parseInt(req.getParameter("leaveType"));

        if (leaveType!= LeaveTypeConstant.SINGLE_DAY||leaveType!=LeaveTypeConstant.SEVERAL_DAYS){

        }
        String studentNum = req.getParameter("studentNum");
        Map<String, Object> data= studentService.findAllInfoByStudentNum(studentNum);
        data.put("leaveType",leaveType);

        req.setAttribute("data",data);
        req.setAttribute("currentTime", DateUtils.format(new Date(),"yyyy-MM-dd HH:mm"));
        req.getRequestDispatcher("include/leave.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
